<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */
class ReportsController extends AppController {

    /**
     * Define $uses
     *
     */

    var $uses=array('User','Item','Meeting','Decision','Userstatus','Attachment');

    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form','Text');
/**
    /**
     * Describe meeting
     *
     * @param $committee
     * @param $id
     * @return null
 */
    function meeting($committee,$id=null){
        $this->Meeting->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        $this->Meeting->Decision->Behaviors->detach('Comment');
        $this->Meeting->Decision->Behaviors->detach('MultiFile');
        
        $this->set('dcommittee',$this->dcommittee);
        $this->layout='report';
        $this->Meeting->Decision->bindLatest();
        $decisions=$this->Meeting->Decision->find('all',array(
            'conditions'=>array('meeting_id'=>$id),
            'order'=>array(
                'Decision.lft asc',
            )
        ));
        
        //check permission
        if(empty($this->permission['Report']['View'])){
            $this->notpermit();
        }else{
            $meeting_permit=$this->meetingPermission($id);
            if(empty($meeting_permit['View'])){
                $this->notpermit();
            }
        }
  
        $decision_id=Set::extract($decisions,'{n}.Decision.id');
        $userstatusfiles=$this->Attachment->userstatusFiles(array('Userstatus.decision_id'=>$decision_id));
        $groupstatusfiles=$this->Attachment->groupstatusFiles(array('Groupstatus.decision_id'=>$decision_id));
        if(!empty($userstatusfiles)) $this->set('userstatusfiles',$userstatusfiles);
        if(!empty($groupstatusfiles)) $this->set('groupstatusfiles',$groupstatusfiles);
        $this->set(compact('decisions',$decisions));
        $this->render('meeting','popup');
    }

    /**
     * Describe project
     *
     * @param $committee
     * @param $id
     * @return null
 */
    function project($committee,$id=null){
        $this->Item->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        $this->Item->Decision->Behaviors->detach('Comment');
        $this->Item->Decision->Behaviors->detach('MultiFile');

        $this->set('dcommittee',$this->dcommittee);
        $this->layout='report';
        $this->Item->Decision->bindLatest();
        $decisions=$this->Item->Decision->find('all',array(
            'conditions'=>array('item_id'=>$id),
            'order'=>array(
                'Decision.ordering asc',
            )
        ));
        
        $decision_id=Set::extract($decisions,'{n}.Decision.id');
        
        //check permission
        if(empty($this->permission['Report']['View'])){
            $this->notpermit();
        }else{
            $ddata=array();
            foreach($decision_id as $d){
                $ddata['Decision'][]['id']=$d;
            }
            $item_permit=$this->itemPermission($ddata);
            if(empty($item_permit['View'])){
                $this->notpermit();
            }
        }
        $userstatusfiles=$this->Attachment->userstatusFiles(array('Userstatus.decision_id'=>$decision_id));
        $groupstatusfiles=$this->Attachment->groupstatusFiles(array('Groupstatus.decision_id'=>$decision_id));
        if(!empty($userstatusfiles)) $this->set('userstatusfiles',$userstatusfiles);
        if(!empty($groupstatusfiles)) $this->set('groupstatusfiles',$groupstatusfiles);
        $this->set(compact('decisions',$decisions));
        $this->render('project','popup');
    }
    
    function status(){
        $this->layout='report';
        $this->Committee->recursive=-1;
        $this->Meeting->Behaviors->detach('Comment');
        $this->Meeting->Decision->Behaviors->detach('MultiFile');
        
        $a=$this->Session->read('a');
        $meeting=$a['meeting'];
        $chairperson=$a['chairperson'];
        $status=$a['status'];
        $from=$a['from'];
        $to=$a['to'];
        $keyword=$a['keyword'];
     
        $not=$this->Committee->deletedDecisionId();
        
        $this->Meeting->recursive=-1;
        $this->Meeting->Decision->Behaviors->detach('Comment');
        
        $this->Meeting->Decision->bindLatest();
        if(!empty($from)){
            $cond[]= "Decision.deadline>='".date("Y-m-d 0-0-0",strtotime($from))."'";
        }
        if(!empty($to)){
            $cond[]="Decision.deadline<='".date("Y-m-d 23-59-59",strtotime($to))."'";
        }
        if(!empty($meeting)){
            $cond[]='Decision.meeting_id='.$meeting;
            $this->set('meeting',$this->Meeting->findById($meeting));
        }
        if(!empty($cond)){
            $decisions=$this->Meeting->Decision->find('all',array(
                'conditions'=>array_values($cond),
                'contain'=>array('User','Group','Userstatus','Groupstatus','Meeting'),
                'order'=>array('Decision.ordering asc')
            ));
        }else{
            $decisions=$this->Meeting->Decision->find('all',array(
                'contain'=>array('User','Group','Userstatus','Groupstatus','Meeting'),
                'conditions'=>array('not'=>array('Decision.id'=>$not),'Decision.committee_id'=>$this->dcommittee['Committee']['id']),
                'order'=>array('Decision.ordering asc')
            ));
        }
        $meeting_id=Set::extract($decisions,'{n}.Decision.meeting_id');
        $meet=$this->Meeting->find('all',array('conditions'=>array('Meeting.id'=>$meeting_id)));
        $meeting_id=set::extract($meet,'{n}.Meeting.id');
        if(!empty($chairperson)){
            $this->set('chairperson',$this->Meeting->User->find('first',array('conditions'=>array('User.id'=>$chairperson))));
            $att=$this->Meeting->Attendance->find('all',array('conditions'=>array('Attendance.Meeting_id'=>$meeting_id,'Attendance.attend_as'=>2,'Attendance.user_id'=>$chairperson)));
            $meeting_id=array_unique(set::extract($att,'{n}.Attendance.meeting_id'));
        }

        foreach($decisions as $i=>$d){
            if(in_array($decisions[$i]['Decision']['id'],$not)){
            //deleted the decision which already had been deleted
                unset ($decisions[$i]);
            }elseif(!in_array($decisions[$i]['Decision']['meeting_id'],$meeting_id)){
                unset ($decisions[$i]);
            }else{
                //deleted the non-implementor status
                $decisions[$i]=$this->Meeting->Decision->unset_nonImpstatus($decisions[$i]);
                //deleted information(decisions which don't have implementor)
                if((empty($decisions[$i]['User'])) && (empty($decisions[$i]['Group']))){
                    unset ($decisions[$i]);
                }
                
            }
            
            if(!empty($status) && !empty($decisions[$i])){
                if($status=='takenontime' ||$status=='takenlate'){
                //if status is 'taken'
                    if($status=='takenontime'){
                        $this->set('status',__('Taken, on time',true));
                        $decisions[$i]=$this->Meeting->Decision->takenTime($decisions[$i],0);
                    }elseif($status=='takenlate'){
                        $this->set('status',__('Taken, late',true));
                        $decisions[$i]=$this->Meeting->Decision->takenTime($decisions[$i],1);
                    }
                    $decisions[$i]=$this->Meeting->Decision->needupdating($decisions[$i],0);
                    
                    //unset the array which don't have status
                    if(empty($decisions[$i]['Userstatus']) && empty($decisions[$i]['Groupstatus'])){
                        unset ($decisions[$i]);
                    }
                    if(empty($decisions[$i]['Userstatus'])){
                        unset ($decisions[$i]['Userstatus']);
                        unset ($decisions[$i]['User']);
                    }
                    if(empty($decisions[$i]['Groupstatus'])){
                        unset ($decisions[$i]['Groupstatus']);
                        unset ($decisions[$i]['Group']);
                    }
                }else{
                    //if status is 'nottaken'
                    if($status=='nottakenlate'){
                        $this->set('status',__('Not taken, overdue',true));
                        if($decisions[$i]['Decision']['deadline']>=date("Y-m-d", mktime())){
                            unset($decisions[$i]);
                        }
                    }elseif($status=='nottaken'){
                        $this->set('status',__('Not taken, before due date',true));
                        if($decisions[$i]['Decision']['deadline']<date("Y-m-d", mktime())){
                            unset($decisions[$i]);
                        }
                    }
                    $decisions[$i]=isset($decisions[$i])?$this->Meeting->Decision->needupdating($decisions[$i],1):null;
                }
            }
            if((empty($decisions[$i]['User'])) && (empty($decisions[$i]['Group']))){
                unset ($decisions[$i]);
            }
        }
        $decision_id=Set::extract($decisions,'{n}.Decision.id');
        $userstatusfiles=$this->Attachment->userstatusFiles(array('Userstatus.decision_id'=>$decision_id));
        $groupstatusfiles=$this->Attachment->groupstatusFiles(array('Groupstatus.decision_id'=>$decision_id));
        if(!empty($userstatusfiles)) $this->set('userstatusfiles',$userstatusfiles);
        if(!empty($groupstatusfiles)) $this->set('groupstatusfiles',$groupstatusfiles);
        $this->set(compact('decisions',$decisions));
        
        $this->render('status','popup');
    }
}

?>
